﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
using System.Data;
using THSERP.BusinessEntities;
namespace THSERP.BaseProvider
{
    public partial class ADCriterasGridControl : THSGridControl
    {
       

        public override void InitGridControlDataSource()
        {
            //base.InitGridControlDataSource();
            DataSet dsCriterias = new ADCriteriasController().GetAllObjectByModuleAndUser(((BaseModuleERP)Screen.Module).ModuleID, THSApp.CurrentUsersInfo.ADUserID);
            BindingSource bds = new BindingSource();
            bds.DataSource =dsCriterias.Tables[0];
            this.DataSource = bds;
            this.RefreshDataSource();
        }
        protected override DevExpress.XtraGrid.Views.Grid.GridView InitializeGridView()
        {
            DevExpress.XtraGrid.Views.Grid.GridView gridView = base.InitializeGridView();
            gridView.OptionsView.ShowGroupPanel = false;
            gridView.OptionsBehavior.AllowDeleteRows =  DevExpress.Utils.DefaultBoolean.True;
            if (gridView.Columns["ADCriteriaName"] != null)
            {
                gridView.Columns["ADCriteriaName"].VisibleIndex = 0;
                gridView.Columns["ADCriteriaName"].Width = 150;
            }
            if (gridView.Columns["ADCriteriaDesc"] != null)
            {
                gridView.Columns["ADCriteriaDesc"].VisibleIndex = 1;
                gridView.Columns["ADCriteriaDesc"].Width = 150;
            }
            return gridView;
        }
        protected override void OnDoubleClick(EventArgs ev)
        {
            DevExpress.XtraGrid.Views.Grid.GridView gridView = (DevExpress.XtraGrid.Views.Grid.GridView)this.Views[0];
            if (gridView != null)
            {
                int iRowHandle = gridView.FocusedRowHandle;
                if (iRowHandle >= 0)
                {
                    DataRow row = gridView.GetDataRow(iRowHandle);
                    if (row != null)
                    {
                        ADCriteriasInfo objADCriteriasInfo = (ADCriteriasInfo)new ADCriteriasController().GetObjectFromDataRow(row);
                        if (objADCriteriasInfo != null)
                        {
                            ((BaseModuleERP)Screen.Module).SearchByCriteriaName(objADCriteriasInfo.ADCriteriaQueryString);
                        }
                    }
                }
            }
        }
        protected override void GridView_KeyUp(object sender, KeyEventArgs e)
        {
            base.GridView_KeyUp(sender, e);
            DevExpress.XtraGrid.Views.Grid.GridView gridview=(DevExpress.XtraGrid.Views.Grid.GridView)sender;
            if(gridview.FocusedRowHandle>=0)
            {
            if (e.KeyCode == Keys.Delete)
            {
                DialogResult dlg=MessageBox.Show("Có phải bạn muốn xóa lịch sử này?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question) ;
                if (dlg== DialogResult.Yes)
                {
                     ADCriteriasController objADCriteriasController= new ADCriteriasController();
                    DataRow row = gridview.GetDataRow(gridview.FocusedRowHandle);
                    if (row != null)
                    {
                        ADCriteriasInfo objADCriteriasInfo = (ADCriteriasInfo)objADCriteriasController.GetObjectFromDataRow(row);
                        if (objADCriteriasInfo != null)
                        {
                            objADCriteriasController.DeleteObject(objADCriteriasInfo.ADCriteriaID); 
                            gridview.DeleteRow(gridview.FocusedRowHandle);
                        }
                        
                    }
                }
            }}
        }
    }
}
